package LeetCode.month02.day45;

import java.util.ArrayDeque;
import java.util.Deque;

public class No150 {
    public static void main(String[] args) {
        System.out.println(evalRPN(new String[]{"4","13","5","/","+"}));
    }
    public static int evalRPN(String[] tokens) {
        int n = tokens.length;
        Deque<Integer> stack = new ArrayDeque<>();
        for (int i = 0; i < n; i++) {
            if(tokens[i].equals("+")){
                int b = stack.poll();
                int a = stack.poll();
                stack.push(a+b);
            }else if(tokens[i].equals("-")){
                int b = stack.poll();
                int a = stack.poll();
                stack.push(a-b);
            }else if(tokens[i].equals("*")){
                int b = stack.poll();
                int a = stack.poll();
                stack.push(a*b);
            }else if(tokens[i].equals("/")){
                int b = stack.poll();
                int a = stack.poll();
                stack.push(a/b);
            }else{
                stack.push(Integer.parseInt(tokens[i]));
            }
        }
        return stack.poll();
    }
}
